package com.sskj.gocoin.v.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.gocoin.R;
import com.sskj.gocoin.adapter.ItemClickSupport;
import com.sskj.gocoin.adapter.slimadapter.IViewInjector;
import com.sskj.gocoin.adapter.slimadapter.SlimAdapter;
import com.sskj.gocoin.adapter.slimadapter.SlimInjector;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.m.bean.FabiDetailListBean;
import com.sskj.gocoin.mvchelper.ModelRx2DataSource;
import com.sskj.gocoin.p.activity.FabiBuyDetailPresenter;
import com.sskj.gocoin.v.activity.FabiDetailActivity;
import com.sskj.gocoin.v.activity.FabiSellDetailActivity;
import com.sskj.gocoin.v.base.BaseFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 购买出售明细列表
 */
public class FabiBuyAndSellDetailFragment extends BaseFragment<FabiDetailActivity, FabiBuyDetailPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter<FabiDetailListBean> slimAdapter;
    private boolean isBuy;
    private MVCCoolHelper<List<FabiDetailListBean>> mvcCoolHelper;

    @Override
    public int getLayoutId() {
        return R.layout.recyclerview;
    }

    @Override
    public FabiBuyDetailPresenter getPresenter() {

        return new FabiBuyDetailPresenter();
    }

    public static FabiBuyAndSellDetailFragment newInstance(boolean isBuy) {

        Bundle args = new Bundle();
        args.putBoolean(Constants.IS_BUY, isBuy);
        FabiBuyAndSellDetailFragment fragment = new FabiBuyAndSellDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("1", App.INSTANCE.getString(R.string.strFabiBuyAndSellDetailFragment201));
        statusMap.put("2", App.INSTANCE.getString(R.string.strFabiBuyAndSellDetailFragment203));
        statusMap.put("3", App.INSTANCE.getString(R.string.strFabiBuyAndSellDetailFragment205));
        statusMap.put("4", App.INSTANCE.getString(R.string.strFabiBuyAndSellDetailFragment207));
        statusMap.put("5", App.INSTANCE.getString(R.string.strFabiBuyAndSellDetailFragment209));
        isBuy = getArguments().getBoolean(Constants.IS_BUY);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setDividerColor(Color.parseColor("#EEEEEE"))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 3)));
        slimAdapter = SlimAdapter.create().register(R.layout.recy_item_fabi_sell_detail, new SlimInjector<FabiDetailListBean>() {
            @Override
            public void onInject(FabiDetailListBean data, IViewInjector injector) {
                injector.text(R.id.tv_id_code, data.getOrder_num())
                        .text(R.id.tv_time, data.getAdd_time())
                        .text(R.id.tv_price, data.getPrice())
                        .text(R.id.tv_num, data.getTotal_num())
                        .text(R.id.tv_deal_num, data.getTotal_price())
                        .text(R.id.tv_charge, isBuy ? "- -" : data.getSxfee())
                        .textColor(R.id.tv_status, ("1".equals(data.getStatus()) || "2".equals(data.getStatus())) ? getResources().getColor(R.color.market_up) : "3".equals(data.getStatus()) ? getResources().getColor(R.color.market_down) : getResources().getColor(R.color.fabi_bottom_sheet_gray_color))
                        .text(R.id.tv_status, statusMap.get(data.getStatus()));  // 明细状态--已付款 未付款
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<FabiDetailListBean> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<FabiDetailListBean>)
                        page -> mPresenter.getList(page, 15, isBuy), 15);
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    FabiSellDetailActivity.start(getActivity(), slimAdapter.getDataItem(position).getOrder_num(), isBuy);
                });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mvcCoolHelper != null) {
            mvcCoolHelper.refresh();
        }
    }
}
